<div id="container">
<?php  if (!$this->allow('list')) : ?>
	<div class="grid_12 t_a_bottom">
		<?php echo $this->translator->translator('Member_Access_deny_desc'); ?>
	</div>
<?php 
	else : 	
$version = Zend_Registry::get('version');	
echo $this->headScript()->setFile($this->baseUrl().'/js/jquery/table-sorter/jquery.dataTables.js');
$permission_arr = array('add_enable' => $this->allow('add','setting','Invoice'), 'edit_enable' => $this->allow('edit','setting','Invoice'), 'delete_enable' => $this->allow('delete','setting','Invoice'), 'search_enable' => $this->allow('search','setting','Invoice'));
?>
<script type="text/javascript">
$(document).ready(function() {

	floatingbar('#floatingbar',"#button_top_bar",null);
	/****************************Sorting Table Start*********************/
	var oTable = $("#sortTable").dataTable({
				"bPaginate": false,
				"bLengthChange": true,
				"bSearchable": true,
				"bFilter": true,
				"bSort": true,
				"bInfo": false,
				"bAutoWidth": true,
				"bProcessing": false,
				"bSortClasses": true,
				"bStateSave": false,
				"bServerSide": false,
				"aaSorting": [[ 1, "asc" ]],				
				"aoColumnDefs": [ 
						{ "bSortable": false, "bSortClasses": false,  "aTargets": [ 0 ] }
					],
				"oLanguage" : { "sSearch": "<?php echo $this->translator->translator('jquery_filter'); ?>" }
			}); 	
	
	$('td', oTable.fnGetNodes()).hover( function() {		
		$(this).parents("tr:first").children('td').addClass( 'highlighted' );
	}, function() {
		$('td.highlighted').removeClass('highlighted');
	} );
	
	/****************************Sorting Table End*********************/
			
		function openLoaderDialog()
		{
			$('#dialog_loader').dialog({
						autoOpen: true,
						modal: true,
						show: 'fade',
						resizable: 'false',				
						open: function(event, ui)
							 {
								$("#progressbar").progressbar({
									value:100
					
								});							
							  }
					});
		}
		
		function closeLoaderDialog()
		{
			$('#dialog_loader').dialog('option','hide','explode').dialog('close');
		}
			
	
	$('input.checkAll_btn').click(function() {
		var self = this;
		if($(self).is(':checked'))
		{		
			$('input.check_btn').attr('checked', true);
		}
		else
		{
			$('input.check_btn').attr('checked', false);
		}
		$('input.check_btn:disabled').attr('checked', false);
	});
	
	$('a.delete_all').click(function() {
		var self = this;
		if($('input.check_btn:checked').length > 0)
		{
			$("#dialog-confirm").html("<?php echo addslashes($this->translator->translator('invoice_list_delete_confirm')); ?>").dialog({
					resizable: false,
					width:400,
					height:170,
					modal: true,
					show: 'fade',
					hide: 'explode',
					buttons: {
							'<?php echo html_entity_decode($this->translator->translator("common_delete_selected"), ENT_QUOTES, "UTF-8"); ?>': function() {
							deleteAll(self);
							$(this).dialog('close');
						},
					'<?php echo html_entity_decode($this->translator->translator("common_cancel"), ENT_QUOTES, "UTF-8"); ?>': function() {
							$(this).dialog('close');
						}
					}
				});		
		}
		else
		{
			var msg = "<?php echo $this->translator->translator('invoice_selected_err'); ?>";
			openMsgDialog(msg);
		}
	});
	
	allFunctions();
});



function openMsgDialog(msg)
{
	$('#dialog_container').html(msg).dialog({
		autoOpen: true,
		modal: false,
		width: 330,
		height: 160,
		show: 'fade',
		resizable: 'false',				
		buttons: { "Ok": function() { $(this).dialog('option', 'hide', 'explode').dialog("close"); } },
		open: function(event, ui)
		 {
			setTimeout(function() 
			{									
				if($('#' + event.target.id).dialog('isOpen'))
				{
					$('#' + event.target.id).dialog('option', 'hide', 'explode').dialog('close');										
				}
			}, 10000);
		  }
	});
}

/* Get the rows which are currently selected */
function fnGetSelected( oTableLocal )
{
	var aReturn = new Array();
	var aTrs = oTableLocal.fnGetNodes();
	
	for ( var i=0 ; i<aTrs.length ; i++ )
	{
		if ( $(aTrs[i]).hasClass('row_selected') )
		{
			aReturn.push( aTrs[i] );
		}
	}
	return aReturn;
}

function checkNonDeletedId(id,id_arr)
{
	var tmp = false;
	for(var i = 0;i < id_arr.length;i++)
	{
		if(id_arr[i] == id)
		{
			tmp = true;
			break;
		}
	}
	return tmp;
}

function deleteThis(self)
{
	var arr = $(self).attr('rel').split('_');
	$.ajax({
		 url: "<?php echo $this->url( array('module'=> 'Invoice', 'controller' => 'setting', 'action'     => 'delete' ), 'adminrout',    true) ?>",
		 type: 'POST',
		 data: { id: arr[0]},
		 beforeSend: function(){
				$(self).html('<img src="application/modules/Administrator/layouts/scripts/images/loader/ajax_loader.gif" height="16" border="0" />');
			 },
		 success: function(response) 
		 {
			//alert(response);
			var json_arr = eval("("+response+")");
			if(json_arr.status == 'ok')
			{
				$(self).parents("li:first").parents("ul:first").parents("td:first").parents("tr:first").addClass('row_selected');
				var oTables = $('#sortTable').dataTable();
				var anSelected = fnGetSelected( oTables );
				oTables.fnDeleteRow( anSelected[0] );
			}
			else
			{
				openMsgDialog(json_arr.msg);
				$(self).html('<img src="application/modules/Administrator/layouts/scripts/images/tools/delete.png" title="<?php echo $this->translator->translator("common_delete_title"); ?>" border="0" />');
			}
		 },
		error: function(xhr, status, error){				
			var msg = "Error! " + xhr.status + " " + error;
			openMsgDialog(msg);
		}
	});
}

function deleteAll(self)
{
	var id_str = '';
	for(var i=0;i < $('input.check_btn:checked').length;i++)
	{
		if(i == 0)
		{
			 id_str += $('input.check_btn:checked:eq('+i+')').val();
		}
		else
		{
			id_str += ','+$('input.check_btn:checked:eq('+i+')').val();
		}
	}
	
	$.ajax({
			url: "<?php echo $this->url( array('module'=> 'Invoice', 'controller' => 'setting', 'action'     => 'deleteall' ), 'adminrout',    true) ?>",
			type: 'POST',
			data: { id_st: id_str},
			beforeSend: function(){
				$(self).html('<span class="icon loader"></span>');
			 },
			success: function(response) 
			{
				//alert(response);
				var json_arr = eval("("+response+")");
				if(json_arr.status == 'ok')
				{
					for(var i= ($('input.check_btn:checked').length-1);i >= 0;i--)
					{	
						if(!checkNonDeletedId($('input.check_btn:checked:eq('+i+')').val(),json_arr.non_del_arr))
						{							
							$('input.check_btn:checked:eq('+i+')').parents("td:first").parents("tr:first").addClass('row_selected');							
						var oTabled = $('#sortTable').dataTable();
						var anSelected = fnGetSelected( oTabled );
						oTabled.fnDeleteRow( anSelected[0] );								
						}
					}
					var oTabled = $('#sortTable').dataTable();
					var anSelected = fnGetSelected( oTabled );
					for(var k = 0; k < anSelected.length; k++)
					{
						oTabled.fnDeleteRow( anSelected[k] );
					}
				}
				else
				{							
					openMsgDialog(json_arr.msg);
				}	
				$(self).html('<span class="icon delete"><?php echo html_entity_decode($this->translator->translator("common_delete_selected"), ENT_QUOTES, "UTF-8"); ?></span>');
			},
			error: function(xhr, status, error){				
				var msg = "Error! " + xhr.status + " " + error ;
				openMsgDialog(msg);
			}
	});	
}
		
function allFunctions()
{
		$('a.delete_btn').unbind('click');	
		$('a.delete_btn').click(function() {
			var self = this;
			$("#dialog-confirm").html("<?php echo addslashes($this->translator->translator('invoice_list_delete_confirm')); ?>").dialog({
				resizable: false,
				width:400,
				height:170,
				modal: true,
				show: 'fade',
				hide: 'explode',
				buttons: {
						'<?php echo html_entity_decode($this->translator->translator("common_delete"), ENT_QUOTES, "UTF-8"); ?>': function() {
						deleteThis(self);
						$(this).dialog('close');
					},
				'<?php echo html_entity_decode($this->translator->translator("common_cancel"), ENT_QUOTES, "UTF-8"); ?>': function() {
						$(this).dialog('close');
					}
				}
			});
			
		});	
		
		$('a.search_btn').click(function(){
			$('#searchForm').submit();
		});	
		
		
		$('select.country_id').change(function() {	
			if($('select.country_id option:selected').val() == 'any' || $('select.country_id option:selected').val() =='')
			{
				$('select.city_id').html('<option value="any"><?php echo $this->translator->translator("common_select"); ?></option>');	
				$('select.state_id').html('<option value="any"><?php echo $this->translator->translator("common_select_state"); ?></option>');
			}
			else
			{	
				$.ajax({
					  url: "<?php echo $this->url( array('module' =>'Invoice','controller' => 'setting', 'action'     => 'states' ), 'adminrout',    true) ?>",
					  type: 'POST',
					  data: { id: $('select.country_id option:selected').val() },
					  beforeSend: function(){						
								$('select.state_id').html('<option value="any"><?php echo $this->translator->translator("common_loading_language"); ?></option>');
								$('select.city_id').html('<option value="any"><?php echo $this->translator->translator("common_select"); ?></option>');			
								$('#state_id_err').html('<img src="application/modules/Administrator/layouts/scripts/images/loader/ajax_loader.gif" height="16" border="0" />');	
							 },
					  success: function(response){
						//alert(response);
						var json_arr = eval("("+response+")");
						var option = '<option value="any"><?php echo $this->translator->translator("common_select_state"); ?></option>';				
						if(json_arr.status == 'ok')
						{
							for(var i = 0;i<json_arr.states.length;i++)
							{
								option += '<option value="'+json_arr.states[i].state_id+'" >'+stripslashes(json_arr.states[i].state_name)+'</option>';
							}
							$('select.state_id').html(option);
						}
						else
						{
							$('select.state_id').html('<option value="any">'+json_arr.msg+'</option>');
						}
						$('#state_id_err').html('');
					  }
				});
			}
		});
		
		$('select.state_id').change(function() {		
					
			if($('select.state_id option:selected').val() == 'any' || $('select.state_id option:selected').val() == '')
			{
				$('select.city_id').html('<option value="any"><?php echo $this->translator->translator("common_select"); ?></option>');
			}
			else
			{
				$.ajax({
					  url: "<?php echo $this->url( array('module' => 'Invoice', 'controller' => 'setting', 'action'     => 'areas' ), 'adminrout',    true) ?>",
					  type: 'POST',
					  data: { id: $('select.state_id option:selected').val() },
					  beforeSend: function(){						
							$('select.city_id').html('<option value="any"><?php echo $this->translator->translator("common_loading_language"); ?></option>');
							$('#city_id_err').html('<img src="application/modules/Administrator/layouts/scripts/images/loader/ajax_loader.gif" height="16" border="0" />');	
							},
					  success: function(response){
						//alert(response);
						var json_arr = eval("("+response+")");
						var option = '<option value="any"><?php echo $this->translator->translator("common_select"); ?></option>';				
						if(json_arr.status == 'ok')
						{
							for(var i = 0;i<json_arr.cities.length;i++)
							{
								option += '<option value="'+json_arr.cities[i].city_id+'" >'+stripslashes(json_arr.cities[i].city)+'</option>';
							}
							$('select.city_id').html(option);
						}
						else
						{
							$('select.city_id').html('<option value="any">'+json_arr.msg+'</option>');
						}
						$('#city_id_err').html('');
					  }
				});
			}
		});
}

function stripslashes(str) 
{
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\0/g,'\0');
	str=str.replace(/\\\\/g,'\\');
	return str;
}
</script>


<div class="title-container">
	<div class="invoice-template-setting"><h1><?php echo $this->translator->translator('invoice_setting_list_page_name'); ?></h1></div>
    
    <div class="button-container">
        <?php if ($permission_arr['add_enable']) : ?>
        <span class="button  ui-widget-content-1"><a href="<?php echo $this->url( array('module' => 'Invoice','controller' => 'setting', 'action'     => 'add' ), 'adminrout',    true) ?>" ><span class="icon add"><?php echo $this->translator->translator('common_add'); ?></span></a></span>
        <?php endif; ?>
        
		<?php if ($permission_arr['delete_enable']) : ?>
        <span class="button  ui-widget-content-1"><a href="javascript: void(0);" class="delete_all"><span class="icon delete"><?php echo html_entity_decode($this->translator->translator("common_delete_selected"), ENT_QUOTES, "UTF-8"); ?></span></a></span>
        <?php endif; ?>
    </div>
</div>
<div id="button_top_bar"></div>
<div class="content-container">
	<div id="sidebar-inner">
    	<?php echo $this->partial('nav_menu.phtml');  ?>
    </div>
    
    <div id="content-inner">
    	<div class="pagination">
        	<?php echo $this->paginationControl($this->datas, 'Sliding', 'top_pagination.phtml'); ?>
        </div>
        
        <div class="chart">
            <div class="data-table">
            	<table id="sortTable" width="100%" align="center" cellpadding="0" cellspacing="0">
                    <thead>
                    	<tr>
                            <th width="1%" class="center"><input type="checkbox" name="checkAll" id="checkAll" class="checkAll_btn" /></th>
                          	<th width="4%"><?php echo $this->translator->translator('common_id'); ?></th>											  
                            <th width="10%"><?php echo $this->translator->translator('invoice_setting_list_module_name'); ?></th>												  
                        	<th width="13%"><?php echo $this->translator->translator('invoice_setting_default_template_name'); ?></th>
                            <th width="15%"><?php echo $this->translator->translator('invoice_setting_paid_template_name'); ?></th>
                            <th width="17%"><?php echo $this->translator->translator('invoice_setting_unpaid_template_name'); ?></th>
                            <th width="15%"><?php echo $this->translator->translator('invoice_setting_delete_template_name'); ?></th>
                      		<th width="16%"><?php echo $this->translator->translator('invoice_setting_cancel_template_name'); ?></th>
                            <th width="9%"><?php echo $this->translator->translator('common_action'); ?></th>
                      	</tr>
                    </thead>
                    <tbody>
                    <?php 
                                                                                                            
                    $j = 1;
                    foreach ($this->datas as $entry): 
                        if($j % 2 == 0)
                        {
                            $onmouseout = "this.bgColor='#FFFFFF';";
                            $background = '#FFFFFF';
                        }
                        else
                        {
                            $onmouseout = "this.bgColor='#F7F7F7';";
                            $background = '#F7F7F7';
                        }													
                    ?>
                   		<tr bgcolor="<?php echo $background; ?>" onmouseover="this.bgColor='#FCF5DD';" onmouseout="<?php echo $onmouseout; ?>">
                     		<td class="center checkbox"><input type="checkbox" name="checkbtn" id="checkbtn"  value="<?php echo $this->escape($entry->id); ?>" class="check_btn" /></td>
                      		<td class="center"><?php echo $this->escape($entry->id); ?></td>												 												  
                      		<td><?php  echo $this->escape($entry->module_name);?></td>
                      		<td><?php  echo $this->escape($entry->default_template);?></td>
                      		<td><?php  echo $this->escape($entry->paid_template);?></td>	
                      		<td><?php  echo $this->escape($entry->unpaid_template);?></td>
                      		<td><?php  echo $this->escape($entry->delete_template);?></td>		
                      		<td><?php  echo $this->escape($entry->cancel_template);?></td>											  
                      		<td class="tools-list">
                            	<ul>
                                	<li>
									<?php if ($permission_arr['edit_enable']) : ?>
                                	<a href="<?php echo $this->url( array('module' => 'Invoice','controller' => 'setting', 'action'     => 'edit', 'id' => $this->escape($entry->id)), 'adminrout',    true) ?>"><img src="application/modules/Administrator/layouts/scripts/images/tools/edit.png" title="<?php echo $this->translator->translator('common_edit_title'); ?>" alt="<?php echo $this->translator->translator('common_edit_title'); ?>"  border="0" /></a>
                                	<?php endif; ?>
                                    </li>
                                    
                                    <li>
                                	<?php if ($permission_arr['delete_enable']) : ?>
                                	<a href="javascript:void(0);" class="delete_btn" rel="<?php echo $this->escape($entry->id); ?>"><img src="application/modules/Administrator/layouts/scripts/images/tools/delete.png" title="<?php echo $this->translator->translator('common_delete_title'); ?>" border="0" /></a>
                                	<?php endif; ?>
                                    </li>
                              	</ul>
							</td>
                    	</tr>
                    <?php 													
                        $j++;
                        endforeach;														
                   	?>
                   	</tbody>
				</table>
            </div>
		</div>
        <div class="pagination">
        	<?php echo $this->paginationControl($this->datas, 'Sliding', 'pagination.phtml'); ?>
        </div>
	</div>
</div>

<div id="floatingbar">
	<h1><?php echo $this->translator->translator('invoice_setting_list_page_name'); ?></h1>
    
    <div class="button-container">
        <?php if ($permission_arr['add_enable']) : ?>
        <span class="button  ui-widget-content-1"><a href="<?php echo $this->url( array('module' => 'Invoice','controller' => 'setting', 'action'     => 'add' ), 'adminrout',    true) ?>" ><span class="icon add"><?php echo $this->translator->translator('common_add'); ?></span></a></span>
        <?php endif; ?>
        
		<?php if ($permission_arr['delete_enable']) : ?>
        <span class="button  ui-widget-content-1"><a href="javascript: void(0);" class="delete_all"><span class="icon delete"><?php echo html_entity_decode($this->translator->translator("common_delete_selected"), ENT_QUOTES, "UTF-8"); ?></span></a></span>
        <?php endif; ?>
    </div>
</div>
<?php endif; ?>

<div id="dialog_container" title="<?php echo $this->translator->translator('common_dialog_msg',$this->translator->translator('invoice_setting_list_page_name')); ?>"></div>
<div id="dialog-confirm" title="<?php echo $this->translator->translator('common_confirmation', $this->translator->translator('common_delete')); ?>"></div>	
<div id="dialog_loader" style="display:none" title="<?php echo $this->translator->translator('common_processing_language'); ?>">
	<div id="progressbar" ></div>	
</div>
</div>